﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    networkstationident.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-02-20  17:39
*/

#include "networkstationident.h"
#include <iostream>
#include <fstream>
#include <algorithm>

NetWorkStationIdent::NetWorkStationIdent(string filePath)
{
    // 打开文件 - 确保路径正确
    std::ifstream inputFile(filePath, std::ios::binary | std::ios::ate); // 使用 'ate' 选项直接定位到文件末尾
    if (!inputFile) {
        std::cerr << "无法打开文件" << std::endl;
    }else{
        // 获取文件大小
        std::streamsize fileSize = inputFile.tellg();
        inputFile.seekg(0, std::ios::beg); // 回到文件开头

        // 计算结构体数量
        size_t structSize = sizeof(NetWorkStation);
        if (fileSize % structSize != 0) {
            std::cerr << "文件大小与结构体大小不匹配" << std::endl;
        }
        size_t numberOfStations = fileSize / structSize;

        // 重置 vector 大小
        stations.resize(numberOfStations);

        // 读取数据
        if (!inputFile.read(reinterpret_cast<char*>(stations.data()), fileSize)) {
            std::cerr << "读取文件失败" << std::endl;
        }
        // 关闭文件
        inputFile.close();
    }
}

NetWorkStationIdent::~NetWorkStationIdent()
{
    stations.clear();
}

vector<NetWorkStation> NetWorkStationIdent::getStations() const
{
    return stations;
}

void NetWorkStationIdent::setStations(const vector<NetWorkStation> &value)
{
    stations = value;
}

bool NetWorkStationIdent::saveToFile(string filePath)
{
    // 打开文件 - 确保路径正确
    std::ofstream outputFile(filePath, std::ios::binary);
    if (!outputFile) {
        std::cerr << "无法打开文件进行写入: " << filePath << std::endl;
        return false;
    }else{
        // 计算需要写入的字节数
        size_t totalSize = stations.size() * sizeof(NetWorkStation);

        // 写入数据
        if (!outputFile.write(reinterpret_cast<const char*>(stations.data()), totalSize)) {
            std::cerr << "写入文件失败" << std::endl;
            outputFile.close();
            return false;
        } else {
            std::cout << "数据成功写入文件: " << filePath << std::endl;
            outputFile.close();
            return true;
        }
    }
}

bool compareStrings(const char* str1, const string& str2) {
    return strncmp(str1, str2.c_str(), str2.size()) == 0 && str1[str2.size()] == '\0';
}

bool NetWorkStationIdent::identify(string &stationType, double freq, double modRate, string modType, string codeType)
{
    if (stations.empty()) {
        stationType = "底数为空";
        return false;
    }



    auto it = std::find_if(stations.begin(), stations.end(), [&](const NetWorkStation &ws) {

        // 计算调制速率的上下限
        double lowerBound = ws.modRate * 0.9;
        double upperBound = ws.modRate * 1.1;

        // 检查调制速率是否在范围内
        bool rateMatch = modRate >= lowerBound && modRate <= upperBound;
        // 检查调制方式是否完全匹配
        bool modTypeMatch = compareStrings(ws.modType, modType);
        // 检查编码方式是否完全匹配
        bool modCodeMatch = compareStrings(ws.codeType, codeType);

        return rateMatch && modTypeMatch && modCodeMatch;
    });

    if (it != stations.end()) {
        // 如果找到匹配项，则将网台类型赋值给 stationType
        stationType.assign(it->station, strlen(it->station));
        return true;
    } else {
        stationType = "未找到匹配的网台";
        return false;
    }
}
